package com.woniu.p2p.behindController;

import com.github.pagehelper.PageInfo;
import com.woniu.p2p.behindService.NewsRecordService;
import com.woniu.p2p.entity.New;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import java.util.Map;

/**
 * NewsRecordController
 *
 *
 */
@Controller
@RequestMapping("/newsRecord")
public class NewsRecordController {

    @Autowired
    NewsRecordService newsRecordService;

    //查询实时咨询
    @RequestMapping("/selectNewRecord")
    public String selectNewsRecord(Map map,String newTitle,Integer pageNum){

        Integer start=pageNum==null?1:pageNum;

        PageInfo<New> pageInfo = newsRecordService.selecNewsByName(newTitle, start);

        map.put("news",pageInfo);
        map.put("newTitle",newTitle);

        return "content/newsRecord";
    }

    //添加新闻
    @RequestMapping("/addNewRecord")
    public String addNewRecord(String trend,String media){
        New news=new New();
        news.setNewTrend(trend);
        news.setNewMedia(media);
        newsRecordService.AddNews(news);
        return "redirect:/newsRecord/selectNewRecord";

    }

    //删除新闻
    @RequestMapping("/delNewsRecord")
    public String delNewsRecord(String ids){
        String[] id = ids.split(",");
        for (String string : id) {
            Integer newId=Integer.parseInt(string);
            newsRecordService.delectNews(newId);

        }
        return "redirect:/newsRecord/selectNewRecord";
    }

    //更新新闻
    @RequestMapping("/updateNewsRecord")
    public String updateNewsRecord(String mytrend,String mymedia,Integer newId){
        New news=new New();
        news.setNewTrend(mytrend);
        news.setNewMedia(mymedia);
        news.setNewId(newId);
        newsRecordService.updateNews(news);
        return "redirect:/newsRecord/selectNewRecord";
    }

}
